package com.ildepinc.persistence.concretes;

import java.io.File;
import java.io.FileReader;
import java.util.Properties;

import com.ildepinc.persistence.abstracts.PropertiesReaderBase;
import com.ildepinc.persistence.constants.PersistenceConstants;

public class PropertiesReader extends PropertiesReaderBase {

  public static Properties getPersistenceProperties() throws Exception {
    Properties properties = new Properties();
    File file = new File(PersistenceConstants.MAIN_PROPERTY_FILE);
    if (file.exists() && file.isFile()) {
      FileReader filereader = new FileReader(PersistenceConstants.MAIN_PROPERTY_FILE);
      properties.load(filereader);
    }
    return properties;
  }

  private String filename = null;
  private File file = null;
  private FileReader filereader = null;

  public PropertiesReader() {
    this.clear();
  }

  public PropertiesReader(String path, String env, String name) throws Exception {
    Properties properties = new Properties();
    this.setPath(path);
    this.setEnv(env);
    this.setName(name);
    this.filename = this.getPath() + File.separator + this.getEnv() + File.separator + this.getName();
    this.file = new File(this.filename);
    if (this.file.exists() && this.file.isFile()) {
      this.filereader = new FileReader(this.filename);
      properties.load(this.filereader);
      setProperties(properties);
    }
  }

  public File getFile() {
    return file;
  }

  public String getFilename() {
    return filename;
  }

  public FileReader getFilereader() {
    return filereader;
  }

  public void setFile(File file) {
    this.file = file;
  }

  public void setFilename(String filename) {
    this.filename = filename;
  }

  public void setFilereader(FileReader filereader) {
    this.filereader = filereader;
  }

  @Override
  protected void clear() {
    super.clear();
    this.filename = null;
    this.file = null;
    this.filereader = null;
  }

}
